package com.heu.blood.common.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Slf4j
@Component
public class ServiceLogAspect {


    @Pointcut("@annotation(com.heu.blood.common.aop.ServiceLogAnnotation)")
    public void operLogPoint(){};

    @Before(value = "operLogPoint()")
    public void logBefore(JoinPoint joinpoint){
        log.info("==============================service log start==============================");
        log.info("方法名："+ joinpoint.getSignature().getName());
        Object[] args = joinpoint.getArgs();
        if (args.length > 0) {
            String parameterType = args[0].getClass().getTypeName();
            log.info("参数值类型：" + parameterType);
            log.info("参数值集合："+ Arrays.asList(joinpoint.getArgs()));
        } else {
            log.info("没有参数传递");
        }
        //获取目标注解对象，logAnnotation
        ServiceLogAnnotation logAnnotation = ((MethodSignature)joinpoint.getSignature()).getMethod().getAnnotation(ServiceLogAnnotation.class);
        String classType = joinpoint.getTarget().getClass().getName();
        log.info("目标注解对象："+ logAnnotation);
        log.info("目标方法所在类："+ classType);
    }

    @AfterReturning(value = "operLogPoint()", returning = "methodResult")
    public void logAfter(JoinPoint joinPoint, Object methodResult){
        if(methodResult == null){
            log.info("返回结果为空");
        }else{
            log.info("返回结果：" + methodResult.toString());
        }

    }


    @After(value = "operLogPoint()")
    public void logFinal() throws Throwable {

    }
}
